package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"strconv"
	"time"
	"yklx2/cache"
	"yklx2/models"
	"yklx2/pkg"
)

type WWW struct {
	beego.Controller
}

type Msg struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (w *WWW) UserAdd() {
	username := w.GetString("username")
	password := w.GetString("password")
	if username == "" || password == "" {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "密码或者用户名不得为空",
			Data: nil,
		}
		w.ServeJSON()
		return
	}

	var err error
	var user models.User
	err = user.DataAdd(username, &user)
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "用户名错误",
			Data: nil,
		}
		w.ServeJSON()
		return
	}

	cacheKey := "cacheID" + username
	cacheCount, _ := cache.Rdb.Get(cache.Ctx, cacheKey).Int()
	if cacheCount >= 3 {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "密码错误3次,请稍后再试",
			Data: nil,
		}
		w.ServeJSON()
		return
	}

	if user.Password != pkg.Md5str1(password) {
		cache.Rdb.Incr(cache.Ctx, cacheKey)
		if cacheCount == 2 {
			cache.Rdb.Set(cache.Ctx, cacheKey, 3, time.Hour*1)
			w.Data["json"] = Msg{
				Code: 0,
				Msg:  "密码错误三次,请稍后再试",
				Data: nil,
			}
			w.ServeJSON()
			return
		}
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "密码错误",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "登录成功",
		Data: user,
	}
	w.ServeJSON()
	return

}

func (w *WWW) RoldQuangxbAdd() {
	rid, _ := w.GetInt("rid", 0)
	qid, _ := w.GetInt("qid", 0)

	if rid == 0 || qid == 0 {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "角色ID和权限iD不得为0",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	data := models.RoldQuangxb{
		Rid: rid,
		Qid: qid,
	}
	_, err := data.RoldQuangxbAdd(&data)
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		w.ServeJSON()
		return
	} else {
		w.Data["json"] = Msg{
			Code: 200,
			Msg:  "添加成功",
			Data: data,
		}
		w.ServeJSON()
	}

}

func (w *WWW) UserRoldgxbList() {
	uid, _ := w.GetInt("uid", 0)
	if uid == 0 {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "用户ID不得为0",
			Data: nil,
		}
		w.ServeJSON()
		return
	}

	var rold models.Rold
	list, err := rold.List(uid)
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "查询失败",
			Data: nil,
		}
		w.ServeJSON()
		return
	}

	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "查询成功",
		Data: list,
	}
	w.ServeJSON()
}

func (w *WWW) RoldQuangxbList() {
	rid, _ := w.GetInt("rid", 0)
	if rid == 0 {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "角色ID不得为0",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	var quan models.Quan
	list, err := quan.QuanList(rid)
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "查询失败",
			Data: nil,
		}
		w.ServeJSON()
		return
	}

	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "查询成功",
		Data: list,
	}
	w.ServeJSON()
}

func (w *WWW) CompanAdd() {
	companame := w.GetString("companame")
	if companame == "" {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "物流公司,名称不得为空",
			Data: nil,
		}
		w.ServeJSON()
		return
	}

	data := models.Compan{
		Companname: companame,
	}
	_, err := data.CompanAdd(&data)
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		w.ServeJSON()
		return
	}

	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "添加成功",
		Data: data,
	}
	w.ServeJSON()
}
func (w *WWW) OrdersAdd() {
	ordername := w.GetString("ordername")
	cid, _ := w.GetInt("cid", 0)
	tid, _ := w.GetInt("tid", 0)

	if ordername == "" || cid == 0 || tid == 0 {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "快递单号,物流公司ID,快递类型ID不得为空",
			Data: nil,
		}
		w.ServeJSON()
		return
	}
	data := models.Orders{
		Ordername: ordername,
		Cid:       cid,
		Tid:       tid,
	}
	_, err := data.OrdersAdd(&data)
	if err != nil {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		w.ServeJSON()
		return
	}

	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "添加成功",
		Data: data,
	}
	w.ServeJSON()
}

func (w *WWW) OrdersList() {

	page, _ := w.GetInt("page", 1)
	size, _ := w.GetInt("size", 3)
	offset := (page - 1) * size
	var list []models.Ordersss

	listkey := "listID" + strconv.Itoa(page)
	cacheList, _ := cache.Rdb.Get(cache.Ctx, listkey).Result()
	if cacheList == "" {
		models.DB.Table("orders").
			Joins("left join compans on compans.id=orders.cid").
			Joins("left join types on types.id=orders.tid").
			Select("orders.id", "orders.ordername", "compans.companname", "types.typess").
			Offset(offset).Limit(size).
			Debug().
			Find(&list)

		cacheJson, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, listkey, cacheJson, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheList), &list)
	}
	var count int64
	models.DB.Model(&models.Orders{}).Count(&count)
	pageData := make(map[string]interface{})
	pageData["count"] = count
	pageData["list"] = list
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "数据查询成功",
		Data: pageData,
	}
	w.ServeJSON()
}

func (w *WWW) OrderDetail() {
	id, _ := w.GetInt("id", 0)
	if id == 0 {
		w.Data["json"] = Msg{
			Code: 0,
			Msg:  "id不为空",
			Data: nil,
		}
		w.ServeJSON()
		return
	}

	var order models.Orders
	var err error

	key := "redisID" + strconv.Itoa(id)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, key).Result()
	if cacheData == "" {
		order, err = order.OrderDatalist(id)
		if err != nil {
			w.Data["json"] = Msg{
				Code: 0,
				Msg:  "查询失败",
				Data: nil,
			}
			w.ServeJSON()
			return
		}
		DataJson, _ := json.Marshal(order)
		cache.Rdb.Set(cache.Ctx, key, DataJson, time.Hour*2)
	} else {
		err = json.Unmarshal([]byte(cacheData), &order)
		if err != nil {
			w.Data["json"] = Msg{
				Code: 0,
				Msg:  "数据查询失败",
				Data: nil,
			}
			w.ServeJSON()
			return
		}
	}
	w.Data["json"] = Msg{
		Code: 200,
		Msg:  "数据查询成功",
		Data: order,
	}
	w.ServeJSON()

}
